package com.copro.chemdb.stat.rule;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.List;

import org.ihelpuoo.db.sql.SQLSegment;
import org.ihelpuoo.localize.Localize;
import org.ihelpuoo.util.Properties;

import com.copro.chemdb.stat.AbstractStatRule;
import com.copro.chemdb.stat.StatResult;
import com.copro.chemdb.subject.Subject;
import com.copro.simple.Application;
import com.copro.simple.SimpleException;
import com.copro.simple.db.QueryCallback;
import com.copro.simple.db.QuickQueryCallback;

public class Rule4 extends AbstractStatRule {

	private String comCode;
	private Properties conf;

	@Override
	public StatResult doStat(Application app, Subject subject, int type)
			throws SimpleException {
		String subCode = subject.getCode();
		SQLSegment sql = app.getSQL(subCode + "list");
		sql.set("comCode", comCode);
		sql.set("type", type);
		List<List<String>> rows = new LinkedList<List<String>>();
		QueryCallback callback = new QuickQueryCallback(rows) {
			@SuppressWarnings("unchecked")
			@Override
			public void exec(ResultSet rs) throws SQLException {
				int count = rs.getMetaData().getColumnCount();
				int[] nums = new int[count - 1];
				int total = 0;
				while (rs.next()) {
					total = 0;
					for (int i = 0; i < count - 1; i++) {
						nums[i] = rs.getInt(i + 2);
						total += nums[i];
					}
					List<String> list = new ArrayList<String>(count);
					list.add(rs.getString(1));
					for (int i = 0; i < count - 1; i++) {
						list.add(total == 0 ? "0" : String
								.valueOf((double) nums[i] / total));
					}
					((List) obj).add(list);
				}
			}
		};
		app.query(sql.toString(), callback);

		sql = app.getSQL(subCode + "count");
		sql.set("comCode", comCode);
		sql.set("type", type);
		List<String> total = new LinkedList<String>();
		callback = new QuickQueryCallback(total) {
			@SuppressWarnings("unchecked")
			@Override
			public void exec(ResultSet rs) throws SQLException {
				int count = rs.getMetaData().getColumnCount();
				int[] nums = new int[count];
				int total = 0;
				while (rs.next()) {
					total = 0;
					for (int i = 0; i < count; i++) {
						nums[i] = rs.getInt(i + 1);
						total += nums[i];
					}
					for (int i = 0; i < count; i++) {
						((List) obj).add(total == 0 ? "0" : String
								.valueOf((double) nums[i] / total));
					}
				}
			}
		};
		app.query(sql.toString(), callback);
		Hashtable<String, Object> result = new Hashtable<String, Object>();

		result.put("rows", rows);
		result.put("total", total);
		result.put("title", Localize.convertAscii2Native(
				conf.getStringTrimed(subject.getCode())).split(","));
		result.put("text", subject.getTitle());
		return new StatResult("rule4", result);
	}

	@Override
	public void setup(Properties conf) {
		this.conf = conf;
		comCode = conf.getStringTrimed("comCode");
	}

}
